import SocketInstance from "../Tools/SocketInstance";
import { RequestCode, UserAction, loginData } from "../GlobalData/DataStruct";
import Tips from "../Tools/Tips";
import DataCenter from "../GlobalData/DataCenter";

export default class login_scene extends Laya.Script {

    constructor() { super(); }
    //登录面板
    private login_panel: Laya.Sprite
    private account_input: Laya.TextInput
    private password_input: Laya.TextInput
    private login_btn: Laya.Button
    private user_panel: Laya.Sprite
    private password_panel: Laya.Sprite
    private login_res_btn: Laya.Button                   //注册

    //注册面板
    private register_panel: Laya.Sprite;
    private account_banner: Laya.Sprite;
    private res_account_input: Laya.TextInput;
    private password_banner: Laya.Sprite;
    private res_password_input: Laya.TextInput;
    private confirm_banner: Laya.Sprite;
    private res_confirm_input: Laya.TextInput;
    private name_banner: Laya.Sprite;
    private res_name_input: Laya.TextInput;
    private register_res_btn: Laya.Button;
    private back_btn: Laya.Button;

    onEnable(): void {
        this.initParams()
        this.initEvents()
    }
    initEvents()  {
        //绑定点击事件
        this.login_btn.on(Laya.Event.CLICK, this, this.onLoginBtn)
        this.login_res_btn.on(Laya.Event.CLICK, this, this.switchPanel)
        this.register_res_btn.on(Laya.Event.CLICK, this, this.onres_RegisterBtn)
        this.back_btn.on(Laya.Event.CLICK, this, this.switchPanel)
        //建议消息监听
        Laya.stage.on("login", this, this.onLoginMsg)
        Laya.stage.on("register", this, this.register_callback)
    }

    /**切换面板 */
    switchPanel() {
        if (this.login_panel.visible) {
            this.login_panel.visible = false
        }
        else {
            this.login_panel.visible = true
        }

        if (this.register_panel.visible) {
            this.register_panel.visible = false
        }
        else {
            this.register_panel.visible = true
        }
    }

    /**注册回调 */
    register_callback(data: any)  {
        //如果data.data为真，则为注册成功
        if (data.data)  {
            this.switchPanel()
        }
        //显示注册信息
        Tips.instance.showTips(data.msg, this.owner)

        this.account_input.text = this.res_account_input.text;
        this.password_input.text = this.res_password_input.text;

    }

    /**检查输入合法性 */
    checkInput(str: string)  {
        console.log(str);
        for (let j = 0; j < DataCenter.checkStr.length; j++)  {
            //search函数返回值如果为-1(没有找到)，就返回
            if (str.search(DataCenter.checkStr[j]) != -1)  {
                return true
            }

        }
    }

    /**注册面板注册按钮 */
    onres_RegisterBtn() {
        //获取账号和密码
        let account_str = this.res_account_input.text
        let password_str = this.res_password_input.text
        let confirm_str = this.res_confirm_input.text
        let name_str = this.res_name_input.text

        if (name_str.length == 0) {
            Tips.instance.showTips("用户名不能为空", this.owner)
            return
        }
        if (name_str.length > 10) {
            Tips.instance.showTips("用户名不能超过10位", this.owner)
            return
        }
        /**检查用户名合法性 */
        if(this.checkInput(name_str)){
            Tips.instance.showTips("用户名不合法",this.owner)
            return
        }
        if (account_str.length < 6) {
            Tips.instance.showTips("账号不能少于6位数", this.owner)
            return
        }
        if (password_str.length < 6) {
            Tips.instance.showTips("密码吗不能少于6位数", this.owner)
            return
        }
        if (password_str != confirm_str) {
            Tips.instance.showTips("两次密码不一致", this.owner)
            return
        }


        //构造注册信息对象
        let login_data=new loginData()
        login_data.account=account_str
        login_data.password=password_str
        login_data.name=name_str
        //发送注册请求
        SocketInstance.instance.sendRequest(
            RequestCode.USER,
            UserAction.SIGNUP,
            login_data
        )
    }

    /**收到信息回调 */
    onLoginMsg(data: any) {
        if (data.code == 200) {
            // //设置全局用户名变量
            // DataCenter.user_name=data.data.name
            // //设置全局登录状态
            // DataCenter.isLogin=true
            //转换场景
            Laya.Scene.open("mainScene.scene")
        }
        else if (data.code == 403) {
            Tips.instance.showTips(data.msg, this.owner)
        }
    }

    initParams()  {
        //登录面板
        this.login_panel = this.owner.getChildByName("login_panel") as Laya.Sprite
        this.user_panel = this.login_panel.getChildByName("account") as Laya.Sprite
        this.account_input = this.user_panel.getChildByName("account_input") as Laya.TextInput
        this.password_panel = this.login_panel.getChildByName("password") as Laya.Sprite
        this.password_input = this.password_panel.getChildByName("password_input") as Laya.TextInput
        this.login_btn = this.login_panel.getChildByName("login_btn") as Laya.Button
        this.login_res_btn = this.login_panel.getChildByName("login_res_btn") as Laya.Button

        //注册面板
        this.register_panel = this.owner.getChildByName("rigister_panel") as Laya.Sprite
        this.account_banner = this.register_panel.getChildByName("account_banner") as Laya.Sprite
        this.res_account_input = this.account_banner.getChildByName("res_account_input") as Laya.TextInput
        this.password_banner = this.register_panel.getChildByName("password_banner") as Laya.Sprite
        this.res_password_input = this.password_banner.getChildByName("res_password_input") as Laya.TextInput
        this.confirm_banner = this.register_panel.getChildByName("confirm_banner") as Laya.Sprite
        this.res_confirm_input = this.confirm_banner.getChildByName("res_confirm_input") as Laya.TextInput
        this.name_banner = this.register_panel.getChildByName("name_banner") as Laya.Sprite
        this.res_name_input = this.name_banner.getChildByName("res_name_input") as Laya.TextInput
        this.register_res_btn = this.register_panel.getChildByName("register_res_btn") as Laya.Button
        this.back_btn = this.register_panel.getChildByName("back_btn") as Laya.Button

    }

    onLoginBtn()  {
        //获取账户信息
        let account_str = this.account_input.text
        let password_str = this.password_input.text

        //账号和密码的验证
        //账号不能小于6位数
        // if(account_str.length<6){
        //     Tips.instance.showTips("账号不能少于六位数",this.owner)
        //     return
        // }

        //创建login_data对象
        let login_data = new loginData()
        login_data.account = account_str;
        login_data.password = password_str;
        //发送消息
        SocketInstance.instance.sendRequest(
            RequestCode.USER,
            UserAction.LOGIN,
            login_data
        )
    }
    onDisable(): void {
    }
}